package com.yequan.thread.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yq
 * @date 2020/05/26
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(6);
        ExecutorService executor = Executors.newFixedThreadPool(10);
        for(int i = 0; i < 5; i++){
            executor.execute(() -> {
                System.out.println("First batch has executed!");
                latch.countDown();
            });
        }
        for(int i = 0; i < 5; i++){
            executor.execute(() -> {
                try {
                    latch.await();
                    System.out.println("Second batch has executed!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        while(latch.getCount() != 1){
            Thread.sleep(1000);
        }
        latch.countDown();
        System.out.println("Wait for first batch finish!");

    }

}
